package com.qf.flash.ui.chat;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.hyphenate.easeui.widget.EaseTitleBar;
import com.qf.dblib.dao.entity.ChatUserBean;
import com.qf.flash.R;
import com.qf.flash.mvp.contract.ContractListContrat;
import com.qf.flash.mvp.presenter.ContatListPresenter;
import com.qf.flash.utils.ACache;
import com.qf.flash.utils.MyUtil;
import com.qf.flashlib.adapter.RVAdapter;
import com.qf.flashlib.base.LazyBaseFragment;

import java.io.Serializable;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 好友列表fragment(带缓存)
 */
public class ContactListFragment extends LazyBaseFragment implements ContractListContrat.View {
    @BindView(R.id.title_bar)
    EaseTitleBar titleBar;
    @BindView(R.id.list)
    RecyclerView list;
    Unbinder unbinder;
    private RVAdapter mAdapter;
    private ContatListPresenter contatListPresenter;
    private static String cacheMode = "FriendList";
    private ACache aCache;
    @Override
    protected int setContentView() {
        return R.layout.activity_new_friends_msg;
    }

    @Override
    protected void initInject() {
        super.initInject();
        contatListPresenter = new ContatListPresenter();
        initPresenters(contatListPresenter);
        aCache =ACache.get(getActivity(),cacheMode);
    }

    @Override
    protected void initView() {
        titleBar.setTitle("好友列表");
        titleBar.setLeftLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getActivity().finish();
            }
        });

        mAdapter = new RVAdapter<>(new ContactItem());
        list.setLayoutManager(new LinearLayoutManager(getContext()));
        list.setAdapter(mAdapter);

        showCache();
        contatListPresenter.getFriendList(1,30);

    }

    @Override
    protected void lazyLoad() {

    }

    private void showCache() {
        List<ChatUserBean> chatUserBeans= (List<ChatUserBean>) aCache.getAsObject(cacheMode);
        List<Object> datas = mAdapter.getDatas();
        if(!MyUtil.isNullOrEmpty(chatUserBeans)){
            datas.addAll(chatUserBeans);
            mAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void setUi(List<ChatUserBean> chatUserBeans) {
        List<Object> datas = mAdapter.getDatas();
        datas.clear();
        datas.addAll(chatUserBeans);
        mAdapter.notifyDataSetChanged();
        aCache.put(cacheMode, (Serializable) chatUserBeans);
    }
//    private EaseTitleBar mybar;
//
//    @Override
//    protected void initView() {
//        super.initView();
//        mybar = getView().findViewById(R.id.title_bar);
//        mybar.setLeftImageResource(R.drawable.common_back);
//        mybar.setLeftLayoutClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                getActivity().finish();
//            }
//        });
//    }
//
//    @Override
//    public void onClick(View view) {
//        getActivity().finish();
//}
}
